package com.zpark.wxshop.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zpark.wxshop.Utils.redisUtil;
import com.zpark.wxshop.Vo.OrderInfoVo;
import com.zpark.wxshop.Vo.ShopVo;
import com.zpark.wxshop.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zpark.wxshop.Utils.Result;
import java.util.UUID;
import com.zpark.wxshop.service.ICartService;
import com.zpark.wxshop.entity.Cart;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author PJone
 * @since 2023-09-04
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    @Resource
    private ICartService cartService;

    @Autowired
    private redisUtil redisUtil;


    /**
    * 新增与修改
    */
    @PostMapping
    public Result save(@RequestBody Cart cart) {
        if (cart.getCid() == null){
            cart.setCreatedTime(new Date(new Date().getTime()));
        }
        cart.setModifiedUser(cart.getCreatedUser());
        cart.setModifiedTime(new Date(new Date().getTime()));
        cartService.saveOrUpdate(cart);
        return Result.success(cart);
    }

    /**
    * 根据id删除
    */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        cartService.removeById(id);
        return Result.success();
    }

    /**
    * 批量删除
    */
    @PostMapping("/del/batch/{ids}")
    public Result deleteBatch(@PathVariable("ids") List<Integer> ids) {
        cartService.removeByIds(ids);
        return Result.success();
    }

    /**
    * 查询所有
    */
    @GetMapping
    public Result findAll() {
        return Result.success(cartService.list());
    }

    /**
    * 根据id查询单个
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(cartService.getById(id));
    }

    /**
    * 分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Result.success(cartService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    /**
     * 根据用户id得到购物车信息
     */
    @GetMapping("/uid/{uid}")
    public Result findByUid(@PathVariable("uid") Integer uid){
        return Result.success(cartService.findByUid(uid));
    }

    /**
     * 查询多个id的购物车
     */
    @GetMapping("/ids/{cids}")
    public Result findByCids(@PathVariable("cids") List<Integer> cids){
        List<Cart> cartList = cartService.listByIds(cids);
        return Result.success(cartList);
    }

    /**
     * 查询选中的购物车
     */
    @GetMapping("/check/{cids}")
    public Result findCheckCids(@PathVariable("cids") List<Integer> cids){
        OrderInfoVo orderInfoVo = cartService.findByCids(cids, get16UUID());
        return Result.success(orderInfoVo);
    }

    /**
     * 添加购物车
     */
    @PostMapping("/addOne")
    public Result addOne(@RequestBody Cart cart) {
        Cart cart1 = cartService.getOne(new QueryWrapper<Cart>().eq("uid", cart.getUid()).eq("pid", cart.getPid()));
        if (cart1 != null){
            cart = cart1;
            cart.setNum(cart.getNum()+1);
        }
        cart.setModifiedUser(cart.getCreatedUser());
        cart.setModifiedTime(new Date(new Date().getTime()));
        cartService.saveOrUpdate(cart);
        return Result.success(cart);
    }

    public  String get16UUID(){
        // 1.开头两位，标识业务代码或机器代码（可变参数）
        int machineId = 11;
        // 2.中间四位整数，标识日期
        SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
        String dayTime = sdf.format(new Date());
        // 3.生成uuid的hashCode值
        int hashCode = UUID.randomUUID().toString().hashCode();
        // 4.可能为负数
        if(hashCode < 0){
            hashCode = -hashCode;
        }
        // 5.算法处理: 0-代表前面补充0; 10-代表长度为10; d-代表参数为正数型
        String value = machineId + dayTime + String.format("%010d", hashCode);
        return value;
    }
}

